package edu.kust.wangyiyun.controller;

import edu.kust.wangyiyun.annotation.Authority;
import edu.kust.wangyiyun.common.Result;
import edu.kust.wangyiyun.entity.AuthorityType;
import edu.kust.wangyiyun.entity.Review;
import edu.kust.wangyiyun.entity.User;
import edu.kust.wangyiyun.service.ReviewService;
import edu.kust.wangyiyun.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: Hxianfei
 * @CreateTime: 2024-09-02  20:56
 * @Description: 商品评价
 */
@Authority(AuthorityType.requireLogin)
@RestController
@RequestMapping("/api/review")
public class ReviewController {

    @Resource
    private ReviewService reviewService;

    /**
     * 评价商品
     */
    @PostMapping
    public Result save(@RequestBody Review review) {
        User user = UserHolder.getUser();
        review.setUserId(Long.valueOf(user.getId()));
        reviewService.save(review);
        return Result.success();
    }

    @GetMapping("/reviews")
    public Result selectReviews(Integer productId) {
        return reviewService.selectByProductId(productId);
    }
}
